﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Management;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using Baimasoft.Common;

public partial class ReLogon : System.Web.UI.Page
{
    BaimasoftFile MyFile = new BaimasoftFile();
    CommClass MyCl = new CommClass();
    UserManage MyUM = new UserManage();
    LogManage MyLog = new LogManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            LogonID.Focus();
        }
    }

    protected void Submit_ServerClick(object sender, EventArgs e)
    {
        int iback = NetCommClass.JudgeAccreditInfo();
        if (iback == 1)
        {
            string sLogonID = MyCl.CutLimitString(LogonID.Text.ToString().Trim());
            string sPassword = MyCl.CutLimitString(Password.Text.ToString().Trim());
            if (sLogonID == "" && sPassword == "")
            {
                LogonID.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('帐号和密码均不能为空!');</script>");
                return;
            }
            if (sLogonID == "")
            {
                LogonID.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('帐号不能为空!');</script>");
                return;
            }
            if (sPassword == "")
            {
                Password.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('密码不能为空!');</script>");
                return;
            }


            string sPsdMd5 = MyFile.Md5(sPassword);
            int back = MyUM.UserLogon(sLogonID, sPsdMd5).Rows.Count;
            if (back == 1)
            {
                string url = Request.Url.Host.ToLower();
                string sVisitIP = NetCommClass.GetIPByDomain(url);
                long lIP = NetCommClass.ConvertIPToLong(sVisitIP);
                int iIfIn = MyUM.JudgeIPIfIn(lIP); //当前IP是否是内网IP
                if (iIfIn == 0) //是外网,需要手机验证码
                {
                    string sRndCode = NetCommClass.GetRandomCode(6);             //系统自动生成指定数目的数字
                    int iBackCode = MyUM.MakerMobileCode(sLogonID, sRndCode);    //临时插入以指定帐户中,当验证结束后,即时空间验证码
                    string sMobile = MyUM.GetMobile(sLogonID);
                    //发送手机验证码
                    ShortMessge MySM = new ShortMessge();
                    int ibackSM = MySM.SendOneSM("0000", "jf", "106575290413", "MAH0010502", "10.254.231.147", "sa", "infox1eies2sps3was4!", sMobile, sRndCode, sLogonID);
                    Response.Redirect("MobileCode.aspx?LogonID=" + sLogonID);  // + "&RndCode=" + sRndCode  
                }
                else
                {
                    DataTable UserInfo = new DataTable();

                    UserInfo = MyUM.GetLogonUserInfo(sLogonID);
                    if (UserInfo.Rows.Count == 0)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('登录失败,请联系管理员!');</script>");
                        return;
                    }
                    Session["Baimasoft_User_LogonID"] = UserInfo.Rows[0][1].ToString().Trim();         //帐户
                    Session["Baimasoft_User_Alias"] = UserInfo.Rows[0][2].ToString().Trim();         //别名 
                    Session["Baimasoft_Pers_ID"] = UserInfo.Rows[0][3].ToString().Trim();       //人员ID

                    Session["Baimasoft_Role_Code"] = UserInfo.Rows[0][4].ToString().Trim();         //功能角色ID
                    Session["Baimasoft_DbRole_Code"] = UserInfo.Rows[0][36].ToString().Trim();         //数据角色ID

                    Session["Baimasoft_First_Page"] = UserInfo.Rows[0][5].ToString().Trim();         //首页
                    Session["Baimasoft_User_BussiID"] = UserInfo.Rows[0][6].ToString().Trim();         //销售网点


                    Session["Baimasoft_User_Effective"] = UserInfo.Rows[0][8].ToString().Trim();    //用户帐户是否有效
                    Session["Baimasoft_Pers_Code"] = UserInfo.Rows[0][9].ToString().Trim();         //员工号

                    Session["Baimasoft_Pers_Name"] = UserInfo.Rows[0][10].ToString().Trim();        //姓名
                    Session["Baimasoft_Dept_ID"] = UserInfo.Rows[0][15].ToString().Trim();        //部门ID 
                    Session["Baimasoft_Dept_Name"] = UserInfo.Rows[0][21].ToString().Trim();        //部门名称

                    Session["Baimasoft_Role_Name"] = UserInfo.Rows[0][31].ToString().Trim();        //功能角色名称
                    Session["Baimasoft_DbRole_Name"] = UserInfo.Rows[0][37].ToString().Trim();        //数据角色名称

                    string sPerID = (string)Session["Baimasoft_Pers_ID"];
                    string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "登录系统成功！";
                    string sIP = Page.Request.UserHostAddress;
                    MyLog.InsertLog("1", sPerID, sContent, sIP);
                    //Response.Write("<script language=javascript>var i=screen.availWidth;var j=screen.availHeight;javascript:window.open('SysManage/index.htm','白马软件后台管理系统','toolbar=no,left=0,top=0,titlebar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=yes,scrolling=no,width='+i+',height='+j+'');window.opener=null;window.close();<" + "/script>");
                    //Response.Redirect("SysManage/index.htm");
                    Response.Redirect("SysManage/right.aspx");
                }
            }
            else
            {
                string sContent = "用户" + sLogonID + "输入帐户时,密码或者帐号不正确！";
                string sIP = Page.Request.UserHostAddress;
                MyLog.InsertLog("1", "0", sContent, sIP);
                LogonID.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('密码或帐号不正确,或者该用户权限被限制,请和系统管理员联系!');</script>");
                return;
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('此系统未正确加载相关项,请与系统管理人员联系!');</script>");
            return;
        }
    }
}
